﻿var TableMain = "";
var TableResult = "";
var TotalPage = 1;
var TablePage = "";
var PageCurrentNo = 0;
var curNo = 0;

function GenerateTable(xml) {
    TableMain = "";
    TableResult = "";
    TablePage = "";

    $(xml).find("search-result").each(function() {
        TotalPage = $(this).find("pages-total").text();

        var query = $(this).find("query").text();
        var files = $(this).find("total-files").text();

        TableMain += "<table >";
        TableMain += "   <tr>";
        TableMain += "       <td><b>Keyword: </b>";
        TableMain += "       </td>";
        TableMain += "       <td>" + query;
        TableMain += "       </td>";
		TableMain += "       <td><b>Total files: </b>";
        TableMain += "       </td>";
        TableMain += "       <td>" + files;
        TableMain += "       </td>";		
        TableMain += "   </tr>";
        TableMain += "</table>";
    });

    var i = 0;
    $(xml).find("result-files file").each(function() {
        curNo = curNo + 1;
        var name = $(this).find("name").text();
        var dwCnt = $(this).find("downloads-count").text();
        var date = $(this).find("upload-date").text();
        var url = $(this).find("url").text();
        var size = $(this).find("size").text();
		var audio = $(this).find("flash-preview-url").text();
	 
        var css = "";

        if (curNo % 2 == 0) {
            css = "oddTable";
        } else {
            css = "evenTable";
        }
		
		var srcPic = "";
		if(audio != null && audio != ""){
			srcPic = "Audio.png";	
		}else{
			srcPic = "DownloadIcon.png";	
		}
		
        TableResult += "<table width='100%' class='" + css + "'>";
        TableResult += "   <tr >"; 
        TableResult += "       <td colspan = '6' style='cursor:pointer;' alt='Listen!!' onclick='FuncAudio(\""+audio+"\")' valign='top'><img id='Img' src='"+srcPic+"' />&nbsp;<span class='LinkCss' onclick=\"OpenLinkDW('" + url + "')\">" + name + "</span>";
        TableResult += "       </td>";
        TableResult += "   </tr>";
        TableResult += "   <tr>";
        TableResult += "       <td  ><b>Upload Date: </b>";
        TableResult += "       </td>";
        TableResult += "       <td  >" + date;
        TableResult += "       </td>";
        TableResult += "       <td  ><b>Downloads Count: </b>";
        TableResult += "       </td>";
        TableResult += "       <td  >" + dwCnt;
        TableResult += "       </td>";
        TableResult += "       <td  ><b>File Size: </b>";
        TableResult += "       </td>";
        TableResult += "       <td  >" + size;
        TableResult += "       </td>";
        TableResult += "   </tr>"; 
		 
        TableResult += "</table>";

        i++;
    });


    if (TotalPage > 1) {
        TablePage += "<table >";
        TablePage += "   <tr>";
        TablePage += "       <td  ><span class='LinkCss' onclick='ClickFirst()'>First</span> |";
        TablePage += "       </td>";

        TablePage += "       <td  ><span class='LinkCss' onclick='ClickPreviousPage()'>Previous</span> |";
        TablePage += "       </td>";

        TablePage += "       <td  ><span class='LinkCss' onclick='ClickNextPage()'>Next</span> |";
        TablePage += "       </td>";

        TablePage += "       <td  ><span class='LinkCss' onclick='ClickLast()'>Last</span>";
        TablePage += "       </td>";
        TablePage += "   </tr>";
        TablePage += "</table>";
    }
    else {
        TablePageNo = "";
    }
    $("#dicResult").html(TableMain);
    $("#dicShow").html(TableResult);
    $("#dicPageNumber").html(TablePage);

    TableMain = "";
    TableResult = "";
    TablePage = "";
}